/*
 * $Id: SimpleQueue.java 9 2006-03-08 10:21:59Z wjx $
 */
package com.someok.utils;

import java.util.LinkedList;
import java.util.NoSuchElementException;

/**
 * Simple Queue (FIFO).
 *
 * <p><a href="SimpleQueue.java.html"><i>View Source</i></a></p>
 *
 * @version $Revision: 9 $ $Date: 2006-03-08 18:21:59 +0800 (星期三, 08 三月 2006) $
 */
public class SimpleQueue {

	private LinkedList list = new LinkedList();

	/**
	 * Puts object in queue.
	 *
	 * @param o      object to be stored in queue
	 */
	public void put(Object o) {
		list.addLast(o);
	}

	/**
	 * Returns an element (object) from queue.
	 *
	 * @return element from queue or null if queue is empty
	 */
	public Object get() {
		Object result = null;
		try {
			result = list.removeFirst();
		} catch (NoSuchElementException nsee) {
		}
		return result;
	}

	/**
	 * Returns all elements from the queue and clears it.
	 *
	 * @return array of objects from queue
	 */
	public Object[] getAll() {
		Object[] res = list.toArray();
		list.clear();
		return res;
	}


	/**
	 * Peek element in queue. Returned elements is not removed from the queue.
	 *
	 * @return peeked object
	 */
	public Object peek() {
		return list.getFirst();
	}

	/**
	 * Is queue empty?
	 *
	 * @return true if queue is empty, otherwise false
	 */
	public boolean isEmpty() {
		return list.isEmpty();
	}
}
